package com.elric.mall.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.elric.mall.common.api.BaseController;
import com.elric.mall.common.api.CommonPage;
import com.elric.mall.common.api.CommonResult;
import com.elric.mall.dto.TaskDto;
import com.elric.mall.enums.ImportType;
import com.elric.mall.model.Task;
import com.elric.mall.model.UmsAdmin;
import com.elric.mall.service.TaskService;
import com.elric.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * 实现功能：计划任务控制层
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 10:44
 */
@Api(tags = "TaskController", description = "计划任务接口")
@RequestMapping("/task")
@RestController
public class TaskController extends BaseController {

    @Autowired
    TaskService taskService;

    @Autowired
    UserService userService;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody TaskDto.Params task, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        if (CollectionUtil.isEmpty(task.getUnitList())) {
            return CommonResult.failed("关联大单位不能为空！");
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        int count = taskService.create(task, umsAdmin);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改数据")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody TaskDto.Params task, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        if (CollectionUtil.isEmpty(task.getUnitList())) {
            return CommonResult.failed("关联大单位不能为空！");
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        int count = taskService.update(task, umsAdmin);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取大单位清单")
    @RequestMapping(value = "/findUnitByTaskId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult findUnitByTaskId(@RequestParam(value = "id")  String id) {
        return CommonResult.success(taskService.findUnitByTaskId(id));
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getItem(@PathVariable String id) {
        return CommonResult.success(taskService.findById(id));
    }

    @ApiOperation("根据ID删除数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable String id) {
        int count = taskService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询所有任务数据")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult findAll() {
        return CommonResult.success(taskService.findAll());
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<Task>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestBody Task task) {
        List<Task> menuList = taskService.list(pageSize, pageNum, task);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("批量导入")
    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                    @RequestBody List<Task> list) {
        return CommonResult.success(taskService.insertBatch(list, type));
    }

    @ApiOperation("查询任务统计数据")
    @RequestMapping(value = "/statistics", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult statistics() {
        return CommonResult.success(taskService.statistics());
    }
}
